#include "LinkQueue.h"
#include <iostream>
#include <stdexcept>

LinkQueue::LinkQueue(){
    frontnode = nullptr;     //初始化队头队尾指针为空
    rearnode = nullptr;
    count = 0;
}

LinkQueue::~LinkQueue(){
    while(frontnode != nullptr){      //逐个弹出队列节点
        pop_front();
    }
}

void LinkQueue::push_back(int data){
    queuenode* newnode = new queuenode(data);     //初始化新的队列节点
    if(frontnode == nullptr){     //空队
        frontnode = newnode;
        rearnode = newnode;
    }
    else{     //非空队
        rearnode -> next = newnode;     //尾插入新节点
        rearnode = newnode;
    }
    count++;     //数量+1
}

void LinkQueue::pop_front(){
    if(count == 0){     //下溢错误
        throw std ::underflow_error("QUEUE UNDERFLOW");
    }
    queuenode* temp = frontnode;     //创建中间节点存储队头界点
    frontnode = frontnode -> next;
    if(frontnode == nullptr){     //更新队尾节点
        rearnode = nullptr;
    }
    delete temp;     //释放空间
    count--;     //数量-1
}


int LinkQueue::front() const{
    if(count == 0){     //下溢错误
        throw std ::underflow_error("QUEUE UNDERFLOW");
    }
    return frontnode -> data;
}